package api

import (
	"net/http"

	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"github.com/yourusername/lvm-manager/internal/host"
)

// HostHandler 宿主机 API 处理器
type HostHandler struct {
	hostManager *host.HostManager
}

// NewHostHandler 创建宿主机 API 处理器
func NewHostHandler(hostManager *host.HostManager) *HostHandler {
	return &HostHandler{
		hostManager: hostManager,
	}
}

// AddHost 添加宿主机
// POST /api/v1/hosts
func (h *HostHandler) AddHost(c *gin.Context) {
	var req struct {
		ID  string `json:"id" binding:"required"`
		URI string `json:"uri" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error":   "Invalid request format",
			"details": err.Error(),
		})
		return
	}

	if err := h.hostManager.AddHost(req.ID, req.URI); err != nil {
		log.Errorf("Failed to add host %s: %v", req.ID, err)
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "Host added successfully",
		"id":      req.ID,
	})
}

// RemoveHost 移除宿主机
// DELETE /api/v1/hosts/:id
func (h *HostHandler) RemoveHost(c *gin.Context) {
	id := c.Param("id")

	if err := h.hostManager.RemoveHost(id); err != nil {
		log.Errorf("Failed to remove host %s: %v", id, err)
		c.JSON(http.StatusNotFound, gin.H{
			"error": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "Host removed successfully",
		"id":      id,
	})
}

// ListHosts 列出所有宿主机
// GET /api/v1/hosts
func (h *HostHandler) ListHosts(c *gin.Context) {
	hosts := h.hostManager.ListHosts()

	c.JSON(http.StatusOK, gin.H{
		"hosts": hosts,
		"count": len(hosts),
	})
}

// GetHost 获取宿主机信息
// GET /api/v1/hosts/:id
func (h *HostHandler) GetHost(c *gin.Context) {
	id := c.Param("id")

	// 获取宿主机连接管理器
	connMgr, err := h.hostManager.GetHost(id)
	if err != nil {
		log.Errorf("Failed to get host %s: %v", id, err)
		c.JSON(http.StatusNotFound, gin.H{
			"error": err.Error(),
		})
		return
	}

	// 构建响应信息
	info := host.HostInfo{
		ID:        id,
		URI:       connMgr.GetURI(),
		Connected: connMgr.IsConnected(),
	}

	if caps := connMgr.GetCapabilities(); caps != nil {
		info.Hypervisor = caps.Hypervisor
		info.Version = caps.LibvirtVersion
	}

	c.JSON(http.StatusOK, info)
}

// ConnectHost 连接到宿主机
// POST /api/v1/hosts/:id/connect
func (h *HostHandler) ConnectHost(c *gin.Context) {
	id := c.Param("id")

	if err := h.hostManager.ConnectHost(id); err != nil {
		log.Errorf("Failed to connect to host %s: %v", id, err)
		c.JSON(http.StatusNotFound, gin.H{
			"error": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "Connected to host successfully",
		"id":      id,
	})
}

// DisconnectHost 断开宿主机连接
// POST /api/v1/hosts/:id/disconnect
func (h *HostHandler) DisconnectHost(c *gin.Context) {
	id := c.Param("id")

	if err := h.hostManager.DisconnectHost(id); err != nil {
		log.Errorf("Failed to disconnect from host %s: %v", id, err)
		c.JSON(http.StatusNotFound, gin.H{
			"error": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "Disconnected from host successfully",
		"id":      id,
	})
}
